﻿using QWManage.DAL;
using QWManage.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace QWManage.Forms
{
    public partial class FrmGetUser : Form
    {
        public UserInfo User { set; get; }
        DepartmentDAL _departmentDAL = new DepartmentDAL();
        public FrmGetUser()
        {
            InitializeComponent();
        }

        private void FrmGetUser_Load(object sender, EventArgs e)
        {
            lvUsers.Items.Clear();
            lvUsers.FullRowSelect = true;
            lvUsers.MultiSelect = false;
            lvUsers.View = View.Details;
            lvUsers.MouseDoubleClick += LvUsers_MouseDoubleClick;

            lvUsers.Columns.Add(new ColumnHeader() { Text = "ID", Width = lvUsers.Width / 3 - 1 });
            lvUsers.Columns.Add(new ColumnHeader() { Text = "名称", Width = lvUsers.Width / 3 - 1 });
            lvUsers.Columns.Add(new ColumnHeader() { Text = "部门", Width = lvUsers.Width / 3 - 1 });
            txtKeywords.KeyUp += TxtKeywords_KeyUp; ;
            setDepartments();
        }

        private void LvUsers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvUsers.GetItemAt(e.X, e.Y);
            if (item == null) return;
            User = item.Tag as UserInfo;
            DialogResult = DialogResult.OK;
        }

        private void TxtKeywords_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            UserDAL userDAL = new UserDAL();
            UsersDepartmentsRDAL usersDepartmentsRDAL = new UsersDepartmentsRDAL();
            DepartmentDAL departmentDAL = new DepartmentDAL();
            lvUsers.Items.Clear();
            List<UserInfo> users = userDAL.GetUserInfos();
            string words = txtKeywords.Text.ToLower().Trim();
            foreach (UserInfo user in users)
            {
                if (string.IsNullOrEmpty(user.Name)) continue;
                if (!user.Name.ToLower().Contains(words) && !user.UserID.ToLower().Contains(words)) continue;
                ListViewItem item = new ListViewItem();
                item.Text = user.UserID;
                item.Tag = user;
                item.SubItems.Add(user.Name);
                List<int> departmentIds = usersDepartmentsRDAL.GetDepartmentsIds(user.UserID);
                string departments = "";
                foreach (int departmentId in departmentIds)
                {
                    DepartmentInfo departmentInfo = departmentDAL.GetDepartmentInfo(departmentId);
                    if (departmentInfo != null)
                        departments += departmentInfo.Name + "   ";
                }
                item.SubItems.Add(departments.Trim());
                lvUsers.Items.Add(item);
            }
        }

        private void setDepartments()
        {
            tvDepartments.HideSelection = false;
            DepartmentInfo department = _departmentDAL.GetSubDepartments(0)[0];
            gboxDepartments.Text = department.Name;
            tvDepartments.NodeMouseClick += TvDepartments_NodeMouseClick;
            foreach (var departmentInfo in _departmentDAL.GetSubDepartments(department.ID))
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Tag = departmentInfo;
                treeNode.Text = departmentInfo.Name;
                tvDepartments.Nodes.Add(treeNode);
                setSubDepartment(treeNode);
            }
            tvDepartments.ExpandAll();
        }

        private void TvDepartments_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left) return;
            tvDepartments.SelectedNode = e.Node;

            lvUsers.Items.Clear();
            DepartmentInfo departmentInfo = e.Node.Tag as DepartmentInfo;
            UsersDepartmentsRDAL dal = new UsersDepartmentsRDAL();
            List<string> ids = dal.GetUserIds(departmentInfo.ID);
            foreach (string id in ids)
            {
                UserDAL userDAL = new UserDAL();
                UserInfo userInfo = userDAL.GetUserInfo(id);
                ListViewItem item = new ListViewItem();
                item.Text = userInfo.UserID;
                item.Tag = userInfo;
                item.SubItems.Add(userInfo.Name);
                item.SubItems.Add(departmentInfo.Name);
                lvUsers.Items.Add(item);
            }
        }


        private void setSubDepartment(TreeNode treeNode)
        {
            DepartmentInfo department = treeNode.Tag as DepartmentInfo;
            foreach (var departmentInfo in _departmentDAL.GetSubDepartments(department.ID))
            {
                TreeNode subTreeNode = new TreeNode();
                subTreeNode.Tag = departmentInfo;
                subTreeNode.Text = departmentInfo.Name;
                treeNode.Nodes.Add(subTreeNode);
                setSubDepartment(subTreeNode);
            }
        }

    }
}
